package com.lute.report;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import com.lute.model.EmployeeAccountingPeriod;
import com.lute.model.EmployeeEntry;

public class LuteClientReport extends LuteAbstractFactoryReport {
	
	private List<String> category;
	private List<String> entryDate;
	private List<String> workDay;

	public LuteClientReport() {}
	
	public LuteClientReport(String reportType, String generatedOn, EmployeeAccountingPeriod period, String from) {
		
		super(reportType,generatedOn, period, from);
		category = new ArrayList<String>();
		entryDate = new ArrayList<String>();
		workDay = new ArrayList<String>();
	}

	public List<String> getCategory() {
		return category;
	}

	public void setCategory(List<String> category) {
		this.category = category;
	}
	
	public List<String> getEntryDate() {
		return entryDate;
	}

	public void setEntryDate(List<String> entryDate) {
		this.entryDate = entryDate;
	}
	
	public List<String> getWorkDay() {
		return workDay;
	}

	public void setWorkDay(List<String> workDay) {
		this.workDay = workDay;
	}
	
	@Override
	public JRDataSource getDataSource(List<Integer> employeeIds, List<String> froms) {
		List<LuteClientReport> summary = new ArrayList<LuteClientReport>();
		List<GregorianCalendar> cals = new ArrayList<GregorianCalendar>();
		
		for(String cal: froms) {
			String tab[] = cal.split("-");
			cals.add(new GregorianCalendar(Integer.parseInt(tab[0]),Integer.parseInt(tab[1])-1,Integer.parseInt(tab[2])));
		}
		
		SimpleDateFormat dateFromater = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		
		for(GregorianCalendar cal: cals) {
			
			List<EmployeeAccountingPeriod> employeePeriods = EmployeeAccountingPeriod.getEmployeeAccountingPeriodsFromDB(employeeIds, cal);
			for(EmployeeAccountingPeriod period: employeePeriods) {
					
					LuteClientReport report = new LuteClientReport(LuteReportName.CLIENT_REPORT.toString(),
																	 dateFormat.format(date), period, dateFromater.format(cal.getTime()));
					
					List<EmployeeEntry> entries = period.getEmployeeEntries();
					for(EmployeeEntry entry: entries) {
						report.getCategory().add(entry.getCompanyCategory().getName()+"\n");
						report.getEntryDate().add(dateFromater.format(entry.getDate().getTime())+"\n");
						report.getWorkDay().add("n/a"+"\n");
					}
					summary.add(report);
			}
		}
		
		JRDataSource ds = new JRBeanCollectionDataSource(summary);
		
		return ds;
	}
}
